﻿using System;
using System.IO;
using System.Web.UI.WebControls;
using Utility;

namespace Thksoft.helpDoc
{
    public partial class Default : System.Web.UI.Page
    {
        #region 数据定义

        private DocumentUtils utils = new DocumentUtils();

        protected DocumentItem CurrentItem
        {
            get
            {
                int index;
                if (int.TryParse(Request.QueryString["ID"], out index))
                {
                    return utils.GetItemByIndex(index, DocumentItems);
                }
                else
                {
                    return DocumentRoot;
                }
            }
        }

        #endregion 数据定义

        #region 帮助项

        public DocumentItemCollection DocumentItems
        {
            get
            {
                // !!!  每次加载目录项（便于重复加载）
                // !!!  发布时取消注释
                if (Application["SK_Scyx_Document_Items"] == null)
                {
                    Application["SK_Scyx_Document_Items"] = utils.LoadFromXml(File.ReadAllText(Server.MapPath("Catelog.xml")));
                }
                return Application["SK_Scyx_Document_Items"] as DocumentItemCollection;
            }
        }

        public DocumentItem DocumentRoot
        {
            get
            {
                if (DocumentItems.Count == 0)
                {
                    throw new InvalidOperationException("帮助内容不存在!");
                }
                return DocumentItems[0];
            }
        }

        public string DocumentItemsString
        {
            get
            {
                if (Application["SK_Scyx_Document_Items_String"] == null)
                {
                    Application["SK_Scyx_Document_Items_String"] = utils.ParseItemString(DocumentItems);
                }
                return Application["SK_Scyx_Document_Items_String"] as string;
            }
        }

        #endregion 帮助项

        protected void Page_Load(object sender, EventArgs e)
        {
            BuildNavigatorTree();
        }

        #region 构造树

        private void BuildNavigatorTree()
        {
            navTree.Nodes.Clear();

            TreeNode root = new TreeNode();
            root.Text = DocumentRoot.Text;
            root.Value = DocumentRoot.Index.ToString();
            root.NavigateUrl = "javascript:ProcessNavNode(" + root.Value + ");";

            BuildNavigatorTree(root, utils.GetChildItems(DocumentRoot, DocumentItems), string.Empty);

            navTree.Nodes.Add(root);
        }

        private void BuildNavigatorTree(TreeNode root, DocumentItemCollection items, string prefix)
        {
            int index = 1;
            foreach (DocumentItem item in items)
            {
                TreeNode node = new TreeNode();
                node.Text = string.Format("<b>{0}{1}</b>&nbsp; {2}", prefix, index, item.Text);
                node.Value = item.Index.ToString();
                root.ChildNodes.Add(node);
                node.NavigateUrl = "javascript:ProcessNavNode(" + node.Value + ");";

                string _prefix = prefix.Length == 0 ? index + "." : (prefix + index + ".");
                BuildNavigatorTree(node, utils.GetChildItems(item, DocumentItems), _prefix);

                index++;
            }
        }

        #endregion 构造树
    }
}